package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.annotation.UserInfoResolver;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IRedisStatisService;
import cn.wolfcode.wolf2w.redis.impl.RedisStatisServiceImpl;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IRedisStatisService statisService;


    @GetMapping("query")
    public Object list(StrategyQuery qo){
        Page<Strategy> list = strategyService.queryPageList(qo);
        return JsonResult.success(list);
    }

    @GetMapping("themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

    @GetMapping("detail")
    public Object detail(String id){
        //阅读数的操作
        statisService.addViewnum(id,1);
        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }

    /**
     * 添加评论
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("addComment")
    public Object addComment(StrategyComment strategyComment, @UserInfoResolver UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);

        //当评论添加成功后,页面上的评论数+1
        statisService.addReplynum(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }

    /**
     * 攻略评论分页
     * @return
     */
    @GetMapping("comments")
    public Object comments(@ModelAttribute("qo") StrategyCommentQuery qo){
       Page<StrategyComment> list = strategyCommentService.queryPageList(qo);
        return JsonResult.success(list);
    }

    /**
     * 评论点赞操作
     * @param cid
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,@UserInfoResolver UserInfo userInfo){
        strategyCommentService.commentThumb(userInfo.getId(),cid);
        return JsonResult.success();
    }

    @GetMapping("statisVo")
    public Object statisVo(String sid){
        //数据回显
        StrategyStatisVO vo = statisService.getStatisVo(sid);
        return JsonResult.success(vo);
    }

    /**
     * 收藏操作
     * @param sid
     * @return true: 收藏成功  false:取消收藏
     */
    @LoginAnnotation
    @PostMapping("favor")
    public Object favor(String sid,@UserInfoResolver UserInfo userInfo){
        Boolean ret = statisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    /**
     * 点赞操作
     * @param sid
     * @return true: 点赞成功  false:点赞失败
     */
    @LoginAnnotation
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@UserInfoResolver UserInfo userInfo){
        Boolean ret = statisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
}